﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LocationTemplateBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILocationTemplateDA locationTemplateDA = DataAccess.CreateLocationTemplateDA();
        private PubParasInfo pubParasInfo;

        public LocationTemplateBLL()
        {
            this.pubParasInfo = null;
        }

        public LocationTemplateBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LocationTemplateBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> locationTemplateID)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<LocationTemplateInfo> locationTemplateInfos = new List<LocationTemplateInfo>();
                LocationTemplateInfo locationTemplateInfo = null;
                foreach (string id in locationTemplateID)
                {
                    locationTemplateInfo = locationTemplateDA.GetLocationTemplateByID(dataBase, tran, id, this.dataBaseID[0]);
                    locationTemplateInfos.Add(locationTemplateInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (locationTemplateDA.DeleteLocationTemplate(dataBase, tran, locationTemplateID, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < locationTemplateID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<LocationTemplateInfo>(dataBase, tran, locationTemplateInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "LOCATION_TEMPLATE", this.dataBaseID);
                        temp = temp + locationTemplateID[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return locationTemplateDA.GetAllLocationTemplate(this.dataBaseID[0]);
        }

        public object GetInfoByID(string locationTemplateID)
        {
            return locationTemplateDA.GetLocationTemplateByID(locationTemplateID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            LocationTemplateQueryEntity locationTemplateQuery = obj as LocationTemplateQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(locationTemplateQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + locationTemplateQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(locationTemplateQuery.LocTemplateID))
            {
                sqlWhere.Add(" LOC_TEMPLATE_ID " + locationTemplateQuery.LocTemplateID);
            }
            return locationTemplateDA.GetLocationTemplateByQueryList(sqlWhere, locationTemplateQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string locationTemplateID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                LocationTemplateInfo locationTemplate = obj as LocationTemplateInfo;
                if (locationTemplateDA.CheckLocationTemplateIDUnique(dataBase, tran, locationTemplate.LocTemplateID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2320_001", this.pubParasInfo.UserLanguage, new List<string> { locationTemplate.LocTemplateID });
                }
                locationTemplate.UpdatedBy = locationTemplate.CreatedBy;
                locationTemplate.CreatedDate = DateTime.Now;
                locationTemplate.UpdatedDate = DateTime.Now;
                if (locationTemplateDA.InsertLocationTemplate(dataBase, tran, locationTemplate, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LocationTemplateInfo>(dataBase, tran, null, locationTemplate, AuditAction.Insert, this.pubParasInfo.UserID, "LOCATION_TEMPLATE", this.dataBaseID);
                    locationTemplateID = locationTemplate.LocTemplateID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return locationTemplateID;
        }

        public string Update(object obj)
        {
            string locationTemplateID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                LocationTemplateInfo locationTemplate = obj as LocationTemplateInfo;
                locationTemplate.UpdatedDate = DateTime.Now;
                LocationTemplateInfo oldLocationTemplateInfo = locationTemplateDA.GetLocationTemplateByID(dataBase, tran, locationTemplate.LocTemplateID, this.dataBaseID[0]);
                if (locationTemplateDA.UpdateLocationTemplate(dataBase, tran, locationTemplate, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LocationTemplateInfo>(dataBase, tran, oldLocationTemplateInfo, locationTemplate, AuditAction.Update, this.pubParasInfo.UserID, "LOCATION_TEMPLATE", this.dataBaseID);
                    locationTemplateID = locationTemplate.LocTemplateID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return locationTemplateID;
        }
    }
}

